package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.db.cache.YhrwCache;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户任务
 */
public class Yhrw {

	public static final String TABLENAME = "用户任务";
	public static final String CLASSNAME = "Yhrw"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 用户_id */
	private int yh_id;
	
	/** 任务id */
	private int rwid;
	
	/** 完成时间 */
	private java.util.Date wcsj;
	
	
	public Yhrw() {
		super();
	}
	
	public Yhrw(int id, int yh_id, int rwid, java.util.Date wcsj) {
		super();
		this.id = id;
		this.yh_id = yh_id;
		this.rwid = rwid;
		this.wcsj = wcsj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getYh_id() {
		return yh_id;
	}
	
	public void setYh_id(int yh_id) {
		this.yh_id = yh_id;
		addUpdateColumn("用户_id",yh_id);
	}
	
	public void changeYh_idWith(int yh_id){
		this.yh_id += yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMin(int yh_id,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMax(int yh_id,int max){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMaxMin(int yh_id,int max,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}	
	public int getRwid() {
		return rwid;
	}
	
	public void setRwid(int rwid) {
		this.rwid = rwid;
		addUpdateColumn("任务id",rwid);
	}
	
	public void changeRwidWith(int rwid){
		this.rwid += rwid;
		setRwid(this.rwid);
	}
	
	public void changeRwidWithMin(int rwid,int min){
		this.rwid += rwid;
		this.rwid = this.rwid < min ? min : this.rwid;
		setRwid(this.rwid);
	}
	
	public void changeRwidWithMax(int rwid,int max){
		this.rwid += rwid;
		this.rwid = this.rwid > max ? max : this.rwid;
		setRwid(this.rwid);
	}
	
	public void changeRwidWithMaxMin(int rwid,int max,int min){
		this.rwid += rwid;
		this.rwid = this.rwid < min ? min : this.rwid;
		this.rwid = this.rwid > max ? max : this.rwid;
		setRwid(this.rwid);
	}	
	public java.util.Date getWcsj() {
		return wcsj;
	}
	
	public void setWcsj(java.util.Date wcsj) {
		this.wcsj = wcsj;
		addUpdateColumn("完成时间",wcsj);
	}
	
	
	//用户_id
	public Yh getYhPkYh_id(){
		return YhCache.getById(yh_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("yh_id", this.yh_id);
        result.put("rwid", this.rwid);
        result.put("wcsj", this.wcsj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("用户_id", this.yh_id);
        result.put("任务id", this.rwid);
        result.put("完成时间", this.wcsj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.yh_id);
		    SK_OutputStream.writeInt(out,this.rwid);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhrw createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhrw yhrw = new Yhrw();
		    yhrw.id = SK_InputStream.readInt(in,null);
		    yhrw.yh_id = SK_InputStream.readInt(in,null);
		    yhrw.rwid = SK_InputStream.readInt(in,null);
		    return yhrw;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhrw> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhrw> yhrws = new ArrayList<Yhrw>();
		for (Map<String, Object> map : list) {
			yhrws.add(createForColumnNameMap(map));
		}
		return yhrws;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhrw createForColumnNameMap(Map<String, Object> map){
    	Yhrw obj = new Yhrw();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yh_id = SK_Map.getInt("用户_id", map);
	    obj.rwid = SK_Map.getInt("任务id", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhrw> createForList(List<Map<String, Object>> list){
    	List<Yhrw> yhrws = new ArrayList<Yhrw>();
		for (Map<String, Object> map : list) {
			yhrws.add(createForColumnNameMap(map));
		}
		return yhrws;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhrw createForMap(Map<String, Object> map){
    	Yhrw obj = new Yhrw();
	    obj.id = SK_Map.getInt("id", map);
	    obj.yh_id = SK_Map.getInt("yh_id", map);
	    obj.rwid = SK_Map.getInt("rwid", map);
        return obj;
    }
    
    public static Yhrw createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhrw> createForJson(List<String> jsons){
    	List<Yhrw> yhrws = new ArrayList<Yhrw>();
    	for(String json : jsons){
    		yhrws.add(createForJson(json));
    	}
    	return yhrws;
    }
    
    /** 延迟插入数据库 */
    public Yhrw insert(){
    	return YhrwCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhrw update(){
    	return YhrwCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhrwCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhrw insertAndFlush(){
    	return YhrwCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhrw updateAndFlush(){
    	return YhrwCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhrwCache.deleteAndFlush(this);
    }
}